print("正在初始化...")
from selenium import webdriver
from selenium.webdriver.common.by import By
import io
from PIL import Image,ImageTk
import tkinter as tk
import processcheck
import os
import subprocess
import time
import sys
import warnings
import requests
from selenium.webdriver.firefox.options import Options
from tqdm import tqdm
warnings.simplefilter('ignore', ResourceWarning)
firefox_options = Options()
firefox_options.add_argument('--headless')
if not os.path.exists("C:\Program Files (x86)\Sangfor\SSL\EasyConnect"):
    print("请检查是否安装easyconnect")
    print("easyconnect下载链接:https://info.uestc.edu.cn/vpn/EasyConnectInstaller_Windwos.zip")
    sys.exit(1)
if not processcheck.find_process_by_name("EasyConnect.exe"):
    subprocess.Popen(["C:\Program Files (x86)\Sangfor\SSL\EasyConnect\EasyConnect.exe"])
print("正在认证中...")
sys.stdout.flush()
tbar = tqdm(range(3), desc='进度条')
sys.stdout.flush()
driver = webdriver.Firefox(options=firefox_options)
tbar.update()
htmli="https://sso.gcc.edu.cn/portal/login.html?redirectUri=http%3A%2F%2Fsso.gcc.edu.cn%2Fsso%2Fcas3%2FAPP056%2Flogin%3Fservice%3Dhttps%253A%252F%252Fvpn.gcc.edu.cn%252Fauth%252Fcas_validate%253Fentry_id%253D2&r=1876726169"
driver.get(htmli)
tbar.update()
element = driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div[2]/div[1]")
location = element.location
size = element.size
screenshot = driver.get_screenshot_as_png()
tbar.update()
# 将截图转换为Image对象并根据元素位置和尺寸裁剪

image = Image.open(io.BytesIO(screenshot))
cropped_image = image.crop((location['x'], location['y'], location['x'] + size['width'], location['y'] + size['height']))
tbar.update()
tbar.close()
print("请扫码")
# 全局变量
root = tk.Tk()
root.attributes('-topmost', True)
# 使用PIL的Image.open打开图片
root.title("二维码")
# 使用ImageTk.PhotoImage将PIL图像转换为Tkinter可以显示的格式

img_tk = ImageTk.PhotoImage(cropped_image)

# 创建Label来显示图片

panel = tk.Label(root)

panel.pack(side="bottom", fill="both", expand="yes")

# 将图片放置到Label上

label_img = tk.Label(panel, image=img_tk)

label_img.pack(side="right", fill="both", expand="yes")

def check_variable():
    """定期检查全局变量should_continue的值"""


    if driver.current_url != htmli:

        # 如果应该停止，则销毁窗口

        root.destroy()

    else:

        # 否则，1秒后再次检查

        root.after(1000, check_variable)


global should_continue
check_variable()

# 进入Tkinter事件循环

root.mainloop()
print("认证成功!请勿关闭...")
while True:
    try:
        requests.get("http://172.22.14.9")
    except:
        continue
    time.sleep(60)